package dd.prog2.exercises.set13.game;

import dd.prog2.exercises.set13.mvc_gui.Model_c4;

public class AI_player {

	private Model_c4 model;

	private int[][] board_map = { 
			{ 1, 2, 3, 3, 3, 2, 1 },
			{ 1, 2, 3, 4, 3, 2, 1 }, 
			{ 2, 3, 4, 5, 4, 3, 2 },
			{ 3, 4, 5, 7, 5, 4, 3 }, 
			{ 5, 6, 7, 8, 7, 6, 5 },
			{ 6, 7, 8, 9, 8, 7, 6 } };

	private int next = 0; // next AI turn(index in the board)

	public AI_player(Model_c4 m) {
		this.model = m;
	}

	public void makeTurn() {
		check_board();
//		System.out.println(" - - - - - - - - - - - - - - - " + next + " - - - - - - - - - - - - - - -");
		model.setNextTurn(next);
	}

	// i*7+j
	public void check_board() {
		next = 0;
		int max = 0;
		// find first enabled button
		// and set its prio to max
		// check all "enabled" button and compare prios to max
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				if (model.buttons[i * 7 + j][1] == 1) { // enabled ?
					int currentPlayer = model.getPlayer();
					int notCurrentPlayer = 0;
					if(currentPlayer == 1)
						notCurrentPlayer = 2;
					else 
						notCurrentPlayer = 1;
					int PlayerBonus = BonusCounter(i, j, currentPlayer, 3); // check for multiple coins in a row
					int OpponentBonus = BonusCounter(i, j, notCurrentPlayer, 2);
//					System.out.println("Opponent "+ OpponentBonus);
//					System.out.println("bonus: "+PlayerBonus);
					if ((board_map[i][j] + PlayerBonus + OpponentBonus) > max) {
						max = board_map[i][j] + PlayerBonus + OpponentBonus;
						next = i * 7 + j;
						if(max > 100){ // found WIN 
							break; // or block Opponent
						}
//						System.out.println("max(" + i + ", " + j + "). next "+ next);
					}
				}
			}
		}
	}

	
	/*
	 * checks if there are any coins of the same color in neigbour rows/diags/columns
	 */
	private int BonusCounter(int row, int col, int currentPlayer, int stepCount) {
//		System.out.println(row+" "+col);
		int tmp_c = col, tmp_r = row; // tmp vars saving col & row
		int bonus = 0; // bonus points for current position in the board
//		int currentPlayer = model.getPlayer();
		boolean foundNext = false; // if 3 coins in a row found, have to set last one to win
		// bonus points
		int stepCounter = 0;
		// max 3 steps or OutOfBounds TODO while() needs to be optimized
		//----------------------------------------------------
		while((stepCounter < stepCount)){ // search in the row(col++ direction)
			col++;
			if(col > 6) // OutOfBounds
				break;
			if(model.buttons[row * 7 + col][0] == currentPlayer) // next coin value is currentPlayer
				bonus++;
			else
				break;
			stepCounter++;
		}
		if(stepCounter == stepCount){ // found WIN
			foundNext = true;
			if(stepCount == 3)
				bonus += 100;
			else
				bonus += 150;
		}	
		//----------------------------------------------------
			stepCounter = 0;
			col = tmp_c; row = tmp_r;
			// max 3 steps or OutOfBounds
			while((stepCounter < stepCount) && !foundNext){ // search in the row(col-- direction)
				col--;
				if(col < 0) // OutOfBounds
					break;
				if(model.buttons[row * 7 + col][0] == currentPlayer) // next coin value is currentPlayer
					bonus++;
				else
					break;
				stepCounter++;
			}
			if(stepCounter == stepCount){ // found WIN 
				foundNext = true;
				if(stepCount == 3)
					bonus += 100;
				else
					bonus += 150;
			}
		//----------------------------------------------------
				stepCounter = 0;
				col = tmp_c; row = tmp_r;
				// max 3 steps or OutOfBounds
				while((stepCounter < stepCount) && !foundNext){ // search in the column(row++ direction)
					row++;
					if(row > 5) // OutOfBounds
						break;
					if(model.buttons[row * 7 + col][0] == currentPlayer) // next coin value is currentPlayer
						bonus++;
					else
						break;
					stepCounter++;
				}
				if(stepCounter == stepCount){ // found WIN 
					foundNext = true;
					if(stepCount == 3)
						bonus += 100;
					else
						bonus += 150;
				}
		//----------------------------------------------------
					stepCounter = 0;
					col = tmp_c; row = tmp_r;
					// max 3 steps or OutOfBounds
					while((stepCounter < stepCount) && !foundNext){ // search in the diag(col++, row++ direction)
						row++; col++;
						if((row > 5) || (col > 6)) // OutOfBounds
							break;
//						System.out.println(row+" !!! "+col);
						if(model.buttons[row * 7 + col][0] == currentPlayer) // next coin value is currentPlayer
							bonus++;
						else
							break;
						stepCounter++;
					}
					if(stepCounter == stepCount){ // found WIN 
						foundNext = true;
						if(stepCount == 3)
							bonus += 100;
						else
							bonus += 150;
					}
		//----------------------------------------------------
						stepCounter = 0;
						col = tmp_c; row = tmp_r;
						// max 3 steps or OutOfBounds
						while((stepCounter < stepCount) && !foundNext){ // search in the diag(col--, row-- direction)
							row--; col--;
							if((row < 0) || (col < 0)) // OutOfBounds
								break;
							if(model.buttons[row * 7 + col][0] == currentPlayer) // next coin value is currentPlayer
								bonus++;
							else
								break;
							stepCounter++;
						}
						if(stepCounter == stepCount){ // found WIN 
							foundNext = true;
							if(stepCount == 3)
								bonus += 100;
							else
								bonus += 150;
						}
		//----------------------------------------------------
							stepCounter = 0;
							col = tmp_c; row = tmp_r;
							// max 3 steps or OutOfBounds
							while((stepCounter < stepCount) && !foundNext){ // search in the diag(col++, row-- direction)
								row--; col++;
								if((row < 0) || (col > 6)) // OutOfBounds
									break;
								if(model.buttons[row * 7 + col][0] == currentPlayer) // next coin value is currentPlayer
									bonus++;
								else
									break;
								stepCounter++;
							}
							if(stepCounter == stepCount){ // found WIN 
								foundNext = true;
								if(stepCount == 3)
									bonus += 100;
								else
									bonus += 150;
							}
		//----------------------------------------------------
								stepCounter = 0;
								// max 3 steps or OutOfBounds
								while((stepCounter < stepCount) && !foundNext){ // search in the diag(col--, row++ direction)
									row++; col--;
									if((row > 5) || (col < 0)) // OutOfBounds
										break;
									if(model.buttons[row * 7 + col][0] == currentPlayer) // next coin value is currentPlayer
										bonus++;
									else
										break;
									stepCounter++;
								}
								if(stepCounter == stepCount){ // found WIN 
									foundNext = true;
									if(stepCount == 3)
										bonus += 100;
									else
										bonus += 150;
								}
		return bonus;
	}
}
